package com.lavida.project.system.review.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lavida.common.utils.DateUtils;
import com.lavida.common.utils.ReviewUtil;
import com.lavida.project.system.emp.domain.Emp;
import com.lavida.project.system.emp.domain.Roster;
import com.lavida.project.system.emp.mapper.EmpMapper;
import com.lavida.project.system.emp.mapper.RosterMapper;
import com.lavida.project.system.review.domain.Review;
import com.lavida.project.system.review.mapper.ReviewMapper;

@Service
public class ReviewServiceImpl implements ReviewService{
	@Autowired
	private ReviewMapper reviewMapper;
	
	@Autowired
	private RosterMapper rosterMapper;
	
	@Autowired
	private EmpMapper empMapper;
	@Override
	public List<Review> selectReview(Review review) {
		reviewMapper.deleteReview();
		List<Roster> rosterList = rosterMapper.selectRosters();
		for(Roster rosters : rosterList) {
			Review review2 = new Review();
			review2.setCompany(rosters.getCompany());
			review2.setFcompany(rosters.getFen_company());
			review2.setDept(rosters.getDept());
			review2.setCgroup(rosters.getGroup());
			review2.setJobId(Integer.valueOf(rosters.getJob_id()));
			review2.setName(rosters.getName());
			review2.setHiredate(rosters.getHiredate());
			review2.setStatus(rosters.getStatus());
			review2.setLeaveTime(rosters.getLeavetime());
			Emp emp = empMapper.selectByJob_id(Integer.valueOf(rosters.getJob_id()));
			System.out.println("员工》》》》》》》》》》》》"+emp.getName());
			int strarth = 0;
			Integer star = emp.getStarthiredate();
			if (star<0 || star == 0) {
				strarth = 0;
			}else {
				strarth = star;
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(calendar.get(Calendar.YEAR), 11, 31);
			Date dates = calendar.getTime();
			System.out.println("dates>>>>>>>>>>>>>>"+dates);
			//计算单位为月
			int mon = DateUtils.getMonthDiff(rosters.getHiredate(),dates);
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+mon);
			int months = mon + strarth;
			int months2 = months + 12;
			int day;
			if(months<0) {
				review2.setEmpAge(0+"");
			}else{
				review2.setEmpAge(months+"");
			}
			Double two = 0.0;
			Double monpaa = 0.0;
			//获取入职日期开始过了多少个月  ，是不是小于12个月
			Integer days = DateUtils.daysreview(rosters.getHiredate());   
			if(mon<12) {
				//今年就是0天
				 two = 0.0;	
				//明年年假  = (今年时间+12个月+入职前工作多少月份 )
				//获得当年的日期 = 获得今年+1年1月1号  — 今年的一月一号
				//
				 monpaa = 5.0;		
			}else {
				//今年剩余天数 * 国家法定年假天数 / 365
				 two= (ReviewUtil.getReview(months,rosters.getHiredate()) * days)/365;
				 monpaa = ReviewUtil.getReview(months2,rosters.getHiredate());
			}
			System.out.println("今年"+two);
			System.out.println("明年"+monpaa);
			review2.setTwenDays(two+"");
			review2.setOneDays(monpaa+"");
			
			reviewMapper.addReview(review2);
		}
		return reviewMapper.selectReview(review);
	}
	@Override
	public long addReview(Review review) {
		// TODO Auto-generated method stub
		return reviewMapper.addReview(review);
	}
	@Override
	public long deleteReview() {
		// TODO Auto-generated method stub
		return reviewMapper.deleteReview();
	}
	@Override
	public Review selectReviewById(Integer job_id) {
		// TODO Auto-generated method stub
		return reviewMapper.selectReviewById(job_id);
	}

}
